package net.pulseproject.commons.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

// FIXME replace with SafeCatchingActionListener
@Deprecated
public abstract class ActionListenerX implements ActionListener {
	
	private static final Log LOG = LogFactory.getLog(ActionListenerX.class);
	
	private final JFrame parent;
	
	private final String actionDescription;
	
	private final IconsCache cache;
	
	public ActionListenerX(final IconsCache cache, final JFrame parent, final String actionDescription) {
		if(cache == null) { throw new IllegalArgumentException("cache == null"); }
		this.cache = cache;
		if(parent == null) { throw new IllegalArgumentException("parent == null"); }
		this.parent = parent;
		if(actionDescription == null) { throw new IllegalArgumentException("actionDescription == null"); }
		this.actionDescription = actionDescription;
	}

	@Override
	public final void actionPerformed(final ActionEvent event) {
		try {
			this.action(event);
		} catch(final Exception e) {
			LOG.error("Could not perform action!", e);
			final ExceptionDialog dialog =
				new ExceptionDialog(this.cache, this.parent, e, this.actionDescription);
			dialog.setVisible(true);
		}
	}
	
	protected abstract void action(final ActionEvent event);
}
